/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UploadDialog.java
 *
 * Created on Feb 10, 2010, 1:03:05 PM
 */

package com.googlecode.mindlistening.gui;

import com.googlecode.mindlistening.AudioRecordingState;
import com.googlecode.mindlistening.ConversationController;
import com.googlecode.mindlistening.PropertiesStorage;
import com.googlecode.mindlistening.UploadListener;
import com.googlecode.mindlistening.UploadState;
import java.awt.BorderLayout;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import org.apache.commons.lang.StringUtils;
import org.jdesktop.swingx.JXErrorPane;

/**
 *
 * @author ryan
 */
public class UploadDialog extends javax.swing.JDialog implements UploadListener {

    private ConversationController controller;
    private PropertiesStorage propStorage;

    private Map<String,UploadPanel> radioToPanelToHostingPanel;



    /** Creates new form UploadDialog */
    public UploadDialog(ConversationController controller, final PropertiesStorage propStorage, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.hostingPanel.setLayout(new BorderLayout());
        radioToPanelToHostingPanel = new HashMap<String, UploadPanel>();
        Red5UploadPanel red5Panel = new Red5UploadPanel();
        YoutubeUploadPanel youtubePanel = new YoutubeUploadPanel();
        this.hostingPanel.add(red5Panel, BorderLayout.CENTER);


        radioToPanelToHostingPanel.put("Red5", red5Panel);
        radioToPanelToHostingPanel.put("Youtube", youtubePanel);

        this.controller = controller;
        this.propStorage = propStorage;
        this.controller.addUploadListener(this);
    }


   public void uploadChange(UploadState state) {
        if (state.getUploadURL() != null) {
            uploadComplete(state.getUploadURL());
        } else {
            this.overallProgressBar.setValue(state.getOverallPercentComplete());
            this.taskProgressBar.setString(state.getMinorTaskName());
            this.taskProgressBar.setStringPainted(true);
            taskProgressBar.setIndeterminate(state.isMinorTaskIndeterminate());
            if (!state.isMinorTaskIndeterminate()) {
                this.taskProgressBar.setValue(state.getMinorTaskPercentComplete());
            }


        }
    }
    private void uploadComplete(String uploadURL) {
        uploadURLEditorPane.setEnabled(true);
        uploadURLEditorPane.setText(createUploadHtml(uploadURL));
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        uploadPanel = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        serverComboBox = new javax.swing.JComboBox();
        red5RadioButton = new javax.swing.JRadioButton();
        vimeoRadioButton = new javax.swing.JRadioButton();
        youtubeRadioButton = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        hostingPanel = new javax.swing.JPanel();
        uploadButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        imagesTextField = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        taskProgressBar = new javax.swing.JProgressBar();
        jLabel9 = new javax.swing.JLabel();
        overallProgressBar = new javax.swing.JProgressBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        uploadURLEditorPane = new javax.swing.JEditorPane();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        uploadPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Upload Conversation"));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Greenbridge Server");

        serverComboBox.setEditable(true);
        serverComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "http://localhost:5984/greenbridge" }));
        serverComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverComboBoxActionPerformed(evt);
            }
        });
        serverComboBox.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                serverComboBoxFocusLost(evt);
            }
        });

        buttonGroup1.add(red5RadioButton);
        red5RadioButton.setSelected(true);
        red5RadioButton.setText("Red5");
        red5RadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                red5RadioButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(vimeoRadioButton);
        vimeoRadioButton.setText("Vimeo");
        vimeoRadioButton.setEnabled(false);
        vimeoRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vimeoRadioButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(youtubeRadioButton);
        youtubeRadioButton.setText("Youtube");
        youtubeRadioButton.setEnabled(false);
        youtubeRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                youtubeRadioButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Media Hosting");

        javax.swing.GroupLayout hostingPanelLayout = new javax.swing.GroupLayout(hostingPanel);
        hostingPanel.setLayout(hostingPanelLayout);
        hostingPanelLayout.setHorizontalGroup(
            hostingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 606, Short.MAX_VALUE)
        );
        hostingPanelLayout.setVerticalGroup(
            hostingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 210, Short.MAX_VALUE)
        );

        uploadButton.setText("Upload");
        uploadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uploadButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Cancel");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Images Directory");
        jLabel2.setToolTipText("A directory that contains images to upload. Images where the date and time taken are between the conversation start and end will be uploaded.");

        imagesTextField.setToolTipText("A directory that contains images to upload. Images where the date and time taken are between the conversation start and end will be uploaded.");

        jButton2.setText("...");
        jButton2.setToolTipText("A directory that contains images to upload. Images where the date and time taken are between the conversation start and end will be uploaded.");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout uploadPanelLayout = new javax.swing.GroupLayout(uploadPanel);
        uploadPanel.setLayout(uploadPanelLayout);
        uploadPanelLayout.setHorizontalGroup(
            uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, uploadPanelLayout.createSequentialGroup()
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(hostingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(uploadPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(uploadPanelLayout.createSequentialGroup()
                                .addComponent(uploadButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1))
                            .addComponent(serverComboBox, 0, 481, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, uploadPanelLayout.createSequentialGroup()
                                .addComponent(imagesTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, uploadPanelLayout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(red5RadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(youtubeRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(vimeoRadioButton)))
                .addContainerGap())
        );
        uploadPanelLayout.setVerticalGroup(
            uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(uploadPanelLayout.createSequentialGroup()
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(serverComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(imagesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(8, 8, 8)
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(youtubeRadioButton)
                    .addComponent(red5RadioButton)
                    .addComponent(vimeoRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hostingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uploadButton)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Progress"));
        jPanel1.setEnabled(false);

        jLabel8.setText("Task");
        jLabel8.setEnabled(false);

        taskProgressBar.setEnabled(false);

        jLabel9.setText("Overall");
        jLabel9.setEnabled(false);

        overallProgressBar.setEnabled(false);

        jScrollPane1.setBorder(null);

        uploadURLEditorPane.setContentType("text/html");
        uploadURLEditorPane.setEditable(false);
        jScrollPane1.setViewportView(uploadURLEditorPane);

        jLabel7.setText("URL");
        jLabel7.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(overallProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                            .addComponent(taskProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(taskProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(overallProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(uploadPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(uploadPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void serverComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverComboBoxActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_serverComboBoxActionPerformed

    private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uploadButtonActionPerformed
        uploadButton.setEnabled(false);
        new Uploader().execute();
}//GEN-LAST:event_uploadButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        this.dispose();
    
    }//GEN-LAST:event_jButton1ActionPerformed

    private void switchHostingPanel(String type) {
        hostingPanel.removeAll();
        JPanel panel = (JPanel)radioToPanelToHostingPanel.get(type);
        hostingPanel.add(panel, BorderLayout.CENTER);
        hostingPanel.invalidate();
        hostingPanel.validate();
        hostingPanel.repaint();
    }


    private void red5RadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_red5RadioButtonActionPerformed
        switchHostingPanel("Red5");
        Red5UploadPanel red5Panel = (Red5UploadPanel)radioToPanelToHostingPanel.get("Red5");
        red5Panel.onGreenbridgeServerChange((String) serverComboBox.getSelectedItem());
    }//GEN-LAST:event_red5RadioButtonActionPerformed

    private void vimeoRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vimeoRadioButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_vimeoRadioButtonActionPerformed

    private void youtubeRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_youtubeRadioButtonActionPerformed
        switchHostingPanel("Youtube");
    }//GEN-LAST:event_youtubeRadioButtonActionPerformed

    private void serverComboBoxFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_serverComboBoxFocusLost
        Red5UploadPanel red5Panel = (Red5UploadPanel)radioToPanelToHostingPanel.get("Red5");
        red5Panel.onGreenbridgeServerChange((String) serverComboBox.getSelectedItem());
    }//GEN-LAST:event_serverComboBoxFocusLost

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        final JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            this.imagesTextField.setText(file.getAbsolutePath());
        }
    }//GEN-LAST:event_jButton2ActionPerformed
        private class Uploader extends SwingWorker<String,Void> {

            @Override
            protected String doInBackground()  {
                try {
                    //String type = buttonGroup1.getSelection().getActionCommand();
                    //buttonGroup1.
                    // BAD BAD BAD!
                    String type = "Red5";
                    if (youtubeRadioButton.isSelected()) {
                        type = "Youtube";
                    }

                    System.out.println("The type is : " + type);

                    File imagesDir = null;
                    if (StringUtils.isNotEmpty(imagesTextField.getText() )) {
                        imagesDir = new File(imagesTextField.getText());
                    }


                    Map properties = radioToPanelToHostingPanel.get(type).retrieveHostingProperties();
                    return controller.uploadConversation((String) serverComboBox.getSelectedItem(), type, imagesDir, properties);
                } catch (final Exception e) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            overallProgressBar.setValue(0);
                            taskProgressBar.setStringPainted(false);
                            taskProgressBar.setIndeterminate(false);
                            taskProgressBar.setValue(0);
                            JXErrorPane.showDialog(e);
                        }
                    });
                    return e.getMessage();
                }
            }

            @Override
            protected void done() {
                try {
                    String location = get();
                    uploadURLEditorPane.setText(createUploadHtml(location));
                    uploadButton.setEnabled(true);
                    taskProgressBar.setStringPainted(false);
                    taskProgressBar.setIndeterminate(false);
                    taskProgressBar.setValue(100);
                    overallProgressBar.setValue(100);
                    propStorage.storeProperty("uploadServerUrl", (String)serverComboBox.getSelectedItem());
                } catch (Exception ex) {
                    
                }
            }


        }


    private String createUploadHtml(String url) {
        return "<html><a href=\"" + url + "\">" + url + "</a></html>";
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel hostingPanel;
    private javax.swing.JTextField imagesTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JProgressBar overallProgressBar;
    private javax.swing.JRadioButton red5RadioButton;
    private javax.swing.JComboBox serverComboBox;
    private javax.swing.JProgressBar taskProgressBar;
    private javax.swing.JButton uploadButton;
    private javax.swing.JPanel uploadPanel;
    private javax.swing.JEditorPane uploadURLEditorPane;
    private javax.swing.JRadioButton vimeoRadioButton;
    private javax.swing.JRadioButton youtubeRadioButton;
    // End of variables declaration//GEN-END:variables

}
